export default {
    data() {
        return {
            stage_id: 0,
            type: 1,
            page: 1,
            limit: 10,
            count: 0,
            dialogVisible: false,              // 对话框是否显示
            tableData: [],
            ruleForm: {
                id: '',
                name: '',
                date: '',
                radio: '1',
                sort: '',
            },
            rules: {
                id: [
                    {required: true, message: '请选择所属专题', trigger: 'blur'},
                ],
                name: [
                    {required: true, message: '请输入期数', trigger: 'blur'},
                ],
                date: [
                    {required: true, message: '请选择起始时间', trigger: 'blur'},
                ]
            },

            type_list: [{
                id: 1,
                label: '乐趣电台'
            }, {
                id: 2,
                label: '闪购'
            }, {
                id: 3,
                label: '非趣不可'
            }, {
                id: 4,
                label: '游轮趣玩'
            }, {
                id: 5,
                label: '周末去哪'
            }],
            date: ''

        }
    },
    /**
     * 进入页面加载
     */
    mounted: function () {
        var that = this;
        //在缓存中获取值
        that.user_name = sessionStorage.getItem("user_name");
        that.user_id = sessionStorage.getItem("user_id");
        that.token = sessionStorage.getItem("access-token");
        if ((!that.token) || (!that.user_id) || (!that.user_name)) {
            that.$router.push('/login');
        }
        that.getList();
    },

    /**
     * 方法
     */
    methods: {

        /**
         * 选择类型
         * @param tab
         */
        handleClick(tab) {
            let that = this;
            that.type = tab;
            that.getList();
        },


        /**
         * 列表
         */
        getList() {
            const that = this;
            that.tableData = [];
            //请求的数据
            const formData = {};
            formData.token = that.token;
            formData.type = that.type;
            //请求邀请者列表
            that.$http.post(that.adminApi.api_url + "/Number/numberlist", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    // 处理成功的结果
                    if (res.body) {
                        for (var i in res.body.list) {
                            if (res.body.list[i].status === 1) {
                                var status_text = '展示';
                            } else {
                                var status_text = '不展示';
                            }
                            that.tableData.push({
                                i: i,
                                id: res.body.list[i].id,
                                number: res.body.list[i].number,
                                create_time: res.body.list[i].create_time,
                                start_time: res.body.list[i].start_time,
                                end_time: res.body.list[i].end_time,
                                sort: res.body.list[i].sort,
                                status: status_text,
                            });
                        }
                        that.count = res.body.count;
                    }
                },
                function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

        /**
         * 关闭期数弹窗
         */
        handleClose() {
            const that = this;
            that.dialogVisible = false;
            that.stage_id = 0;
            that.ruleForm = {
                id: '',
                name: '',
                date: '',
                radio: '1',
                sort: '',
            }
        },

        /**
         * 提交数据
         */
        onSubmit: function (formName) {
            const that = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (that.stage_id === 0) {
                        that.addState();
                    } else {
                        that.editState();
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },

        /**
         * 添加期数
         */
        addState: function () {
            const that = this;
            //请求的数据
            const formData = {};
            formData.token = that.token;
            formData.type = that.ruleForm.id;
            formData.number = that.ruleForm.name;
            if (that.ruleForm.date !== '') {
                formData.start_time = that.formatDateTime(that.ruleForm.date[0]);
                formData.end_time = that.formatDateTime(that.ruleForm.date[1]);
            }
            formData.status = that.ruleForm.radio;
            formData.sort = that.ruleForm.sort;

            //请求邀请者列表
            that.$http.post(that.adminApi.api_url + "/Number/addnumber", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    // 处理成功的结果
                    if (res.body) {
                        that.$message({
                            type: 'success',
                            message: `操作提示: ${ '添加成功' }`
                        });
                        that.dialogVisible = false;
                        that.ruleForm = {
                            id: '',
                            name: '',
                            date: '',
                            radio: '1',
                            sort: '',
                        },
                            that.getList();
                    }
                },
                function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

        /**
         * 关联产品
         */
        toRelation(e) {
            const that= this;
            that.$router.push({path:'/relation/relation',query:{stage_id:e,type:that.type}})
        },

        /**
         * 查看关联产品
         */
        to_list(e) {
            const that= this;
            that.$router.push({path:'/relation/show_relation',query:{stage_id:e,type:that.type}})
        },

        /**
         * 下一页
         * @param currentPage
         */
        handleCurrentChange: function (currentPage) {
            var that = this;
            that.page = currentPage;
        },


        /**
         * 时间转换
         * @param date
         * @returns {string}
         */
        formatDateTime: function (date) {
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            m = m < 10 ? ('0' + m) : m;
            var d = date.getDate();
            d = d < 10 ? ('0' + d) : d;
            var h = date.getHours();
            var minute = date.getMinutes();
            minute = minute < 10 ? ('0' + minute) : minute;
            h = h < 10 ? ('0' + h) : h;
            return y + '-' + m + '-' + d + ' ' + h + ':' + minute;
        },

        /**
         * 编辑弹窗显示
         */
        edit: function (e) {
            const that = this;
            console.log(e);
            that.stage_id = e;
            that.dialogVisible = true;
            //请求的数据
            const formData = {};
            formData.token = that.token;
            formData.stage_id = that.stage_id;

            //请求邀请者列表
            that.$http.post(that.adminApi.api_url + "/Number/show_edit", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    // 处理成功的结果
                    if (res.body) {
                        const date1 = [];
                        date1[0] = new Date(res.body.start_time);
                        date1[1] = new Date(res.body.end_time);
                        that.ruleForm = {
                            id: res.body.type,
                            name: res.body.number,
                            date: date1,
                            radio: res.body.status.toString(),
                            sort: res.body.sort,
                        }
                    }
                },
                function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },


        /**
         * 修改期数
         */
        editState: function () {
            const that = this;
            //请求的数据
            const formData = {};
            formData.token = that.token;
            formData.type = that.ruleForm.id;
            formData.stage_id = that.stage_id;
            formData.number = that.ruleForm.name;
            if (that.ruleForm.date !== '') {
                formData.start_time = that.formatDateTime(that.ruleForm.date[0]);
                formData.end_time = that.formatDateTime(that.ruleForm.date[1]);
            }
            formData.status = that.ruleForm.radio;
            formData.sort = that.ruleForm.sort;

            //请求邀请者列表
            that.$http.post(that.adminApi.api_url + "/Number/editnumber", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    // 处理成功的结果
                    if (res.body) {
                        that.$message({
                            type: 'success',
                            message: `操作提示: ${ '修改成功' }`
                        });
                        that.dialogVisible = false;
                        that.stage_id = 0;
                        that.ruleForm = {
                            id: '',
                            name: '',
                            date: '',
                            radio: '1',
                            sort: '',
                        };
                        that.getList();
                    }
                },
                function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },
    }
}
  